import Util from '../../store/util/index.js';

const UserSession = {
  login: params => {
    localStorage.setItem('user', JSON.stringify(params));
  },
  logout: params => {
    localStorage.removeItem('user');
    localStorage.removeItem('roles');
  },
  isLogin: params => {
    return localStorage.getItem('user') !== undefined && localStorage.getItem('user') !== null;
  },
  getInfo: () => {
    let user = localStorage.getItem('user');
    return JSON.parse(user);
  },
  setRoles: (userRoles, vm) => {
    let authIds = [];
    try {
      userRoles.forEach(role => {
        if (role.authIds && role.authIds instanceof Array) {
          authIds = authIds.concat(role.authIds);
        }
      });
    } catch (err) {
      // console.error(err);
    }
    let auths = [...new Set(authIds)];
    localStorage.setItem('roles', JSON.stringify(auths));
    vm.$store.commit('updateFirstLevelMenulist');
  },
  getDefaultColumn: (params) => {
    return Util.getDefaultColumn(params);
  },
  getRoles: params => {
    let roles = localStorage.getItem('roles');
    return JSON.parse(roles);
  },
  isSuperAdmin: () => {
    let user = UserSession.getInfo();
    return user.superAdmin;
  }
};

export default UserSession;
